package com.shell.customer.cfca.service;

import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.cfca.auth.CfcaBankUtil;
import com.shell.bee.cfca.auth.request.EntInfoRequest;
import com.shell.bee.cfca.auth.request.EntPayRequest;
import com.shell.bee.cfca.auth.request.PersonInfoRequest;
import com.shell.bee.cfca.auth.response.AuthDataResponse;
import com.shell.bee.cfca.auth.service.CfcaAuthService;
import com.shell.bee.cfca.base.po.CfcaAcctAuth;
import com.shell.bee.cfca.base.service.CfcaAcctAuthService;
import com.shell.customer.accreditation.vo.UserVo;
import com.shell.customer.po.CusAcct;
import com.shell.customer.po.CusRec;
import cpcn.dsp.institution.api.util.GUIDGenerator;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("cfcaAuthTxService")
public class CfcaAuthTxService {
	@Autowired
	private CfcaAcctAuthService cfcaAcctAuthService;
	@Autowired
	private CfcaAuthService cfcaAuthService;

	public String authEnt(CusRec cusRec, CusAcct cusAcct, boolean isAuthPay) throws Exception {
		EntInfoRequest entInfoRequest = new EntInfoRequest();
		entInfoRequest.setTxCode("1304");
		entInfoRequest.setTxSn(GUIDGenerator.genGUID());
		entInfoRequest.setEntName(cusRec.getCusName());
		if (cusRec.getIdType().equals("ent_id_type_1")) {
			entInfoRequest.setEntType("20");
			entInfoRequest.setEntCode(cusRec.getSocCrCode());
		} else if (cusRec.getIdType().equals("ent_id_type_2")) {
			entInfoRequest.setEntType("10");
			entInfoRequest.setEntCode(cusRec.getLisNo());
		}
		entInfoRequest.setLrName(cusRec.getLrName());
		if (!"id_type_1".equals(cusRec.getLrIdType())) {
			return "������CFCA��������������������������";
		}
		entInfoRequest.setLrIdType("0");
		entInfoRequest.setLrIdNo(cusRec.getLrLisNo());

		AuthDataResponse authDataResponse = this.cfcaAuthService.authEntInfo(entInfoRequest);
		if ((!"2000".equals(authDataResponse.getCode())) || (!"20".equals(authDataResponse.getVerification()))) {
			if ("30".equals(authDataResponse.getVerification())) {
				return "������CFCA��" + authDataResponse.getVerification() + "��������������������";
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getResponseMessage())) {
				return "������CFCA��" + authDataResponse.getCode() + authDataResponse.getResponseMessage();
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getMessage())) {
				return "������CFCA��" + authDataResponse.getCode() + authDataResponse.getMessage();
			}
			return "������CFCA��" + authDataResponse.getCode() + "������������������������������������";
		}
		return null;
	}

	public String authEntPayApply(String custId, CusAcct cusAcct) throws Exception {
		EntPayRequest payRequest = new EntPayRequest();
		payRequest.setTxCode("1305");
		payRequest.setTxSn(GUIDGenerator.genGUID());
		payRequest.setAccountName(cusAcct.getAcctName());
		payRequest.setAccountNumber(cusAcct.getAcctNo());
		payRequest.setBankId(CfcaBankUtil.getBankId(cusAcct.getBankName()));
		payRequest.setBranchName(cusAcct.getBankName());
		payRequest.setProvince(cusAcct.getBankProvince());
		if (StringUtil.emptyAndNull(cusAcct.getBankCity())) {
			payRequest.setCity(cusAcct.getBankProvince());
		} else {
			payRequest.setCity(cusAcct.getBankCity());
		}
		AuthDataResponse authDataResponse = this.cfcaAuthService.authEntPayApply(payRequest);
		if ((!"2000".equals(authDataResponse.getCode())) || (!"20".equals(authDataResponse.getStatus()))) {
			if ("30".equals(authDataResponse.getStatus())) {
				return authDataResponse.getStatus() + "��������������������";
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getResponseMessage())) {
				return authDataResponse.getCode() + authDataResponse.getResponseMessage();
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getMessage())) {
				return authDataResponse.getCode() + authDataResponse.getMessage();
			}
			return authDataResponse.getCode() + "��������������������,������������";
		}
		saveOrUpdateAcctAuth(authDataResponse.getTxSN(), cusAcct.getAcctNo(), custId, "1");

		return null;
	}

	public int saveOrUpdateAcctAuth(String txSn, String acctNo, String custId, String state) {
		CfcaAcctAuth sacctAuth = (CfcaAcctAuth) this.cfcaAcctAuthService.selectByKey(acctNo);
		if (sacctAuth == null) {
			sacctAuth = new CfcaAcctAuth();
			sacctAuth.setAcctNo(acctNo);
			sacctAuth.setCustId(custId);
		}
		sacctAuth.setAuthState(state);
		sacctAuth.setPayAuthNum(Integer.valueOf(0));
		sacctAuth.setTxSn(txSn);
		String payAmount = SpringContextUtil.getProperty("cfca.acct.pay.amount");
		sacctAuth.setPayAmt(payAmount);
		return this.cfcaAcctAuthService.saveOrUpdate(sacctAuth);
	}

	public String authUser(UserVo userVo) throws Exception {
		PersonInfoRequest request = new PersonInfoRequest();
		request.setCertNo(userVo.getCertNo());
		if (!"id_type_1".equals(userVo.getCertType())) {
			return "������CFCA����������������������";
		}
		request.setCertType("0");
		request.setUserName(userVo.getUserName());

		request.setTxCode("2124");
		request.setPhoneNo(userVo.getPhoneNo());
		request.setTxSn(GUIDGenerator.genGUID());

		AuthDataResponse authDataResponse = this.cfcaAuthService.authPersonInfo(request);
		if ((!"2000".equals(authDataResponse.getCode())) || (!"20".equals(authDataResponse.getVerification()))) {
			if ("10".equals(authDataResponse.getVerification())) {
				return "������CFCA��" + authDataResponse.getVerification() + "����������������";
			}
			if ("30".equals(authDataResponse.getVerification())) {
				return "������CFCA��" + authDataResponse.getVerification() + "������������������";
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getMessage())) {
				return "������CFCA��" + authDataResponse.getCode() + authDataResponse.getMessage();
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getResponseMessage())) {
				return "������CFCA��" + authDataResponse.getCode() + authDataResponse.getResponseMessage();
			}
			return "������CFCA��" + authDataResponse.getCode() + "����������������������������������";
		}
		return null;
	}

	public String authEntPayConfirm(String custId, String acctNo, BigDecimal amount) throws Exception {
		CfcaAcctAuth acctAuth = this.cfcaAcctAuthService.getAcctAuth(custId, acctNo);
		if ("0".equals(acctAuth.getAuthState())) {
			return "1";
		}
		EntPayRequest entPayRequest = new EntPayRequest();
		entPayRequest.setTxCode("1306");
		entPayRequest.setTxSn(GUIDGenerator.genGUID());
		entPayRequest.setOriginalTxSn(acctAuth.getTxSn());
		entPayRequest.setAmount(String.valueOf(amount));

		AuthDataResponse authDataResponse = this.cfcaAuthService.authEntPayConfirm(entPayRequest);
		if ((!"2000".equals(authDataResponse.getCode())) || (!"20".equals(authDataResponse.getVerification()))) {
			if ("30".equals(authDataResponse.getVerification())) {
				return authDataResponse.getVerification() + "����������������";
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getResponseMessage())) {
				return authDataResponse.getCode() + authDataResponse.getResponseMessage();
			}
			if (!StringUtil.emptyAndNull(authDataResponse.getMessage())) {
				return authDataResponse.getCode() + authDataResponse.getMessage();
			}
			return authDataResponse.getCode() + "��������������������������";
		}
		acctAuth.setAuthState("0");

		acctAuth.setPayAuthNum(Integer.valueOf(acctAuth.getPayAuthNum().intValue() + 1));
		this.cfcaAcctAuthService.updateNotNull(acctAuth);

		return "0";
	}
}
